package com.bsj.media.tool;

import com.alibaba.fastjson.JSON;
import com.bsj.media.network.websocket.CmdConstant;
import com.bsj.power.common.config.exception.ApiException;
import com.bsj.power.common.config.global.JsonResultEnum;
import com.bsj.power.common.config.rocketmq.producer.BaseMqPushUtil;
import com.bsj.power.common.constant.MqTopicAndTagConstants;
import com.bsj.power.common.def.dto.media.BackVodCtrolParam;
import com.bsj.power.common.def.dto.media.BackVodParam;
import com.bsj.power.common.def.dto.media.RealCtrolParam;
import com.bsj.power.common.def.dto.media.RealVideoParam;
import com.bsj.power.common.def.dto.video.BackVodControl;
import com.bsj.power.common.def.dto.video.CloseConnectionParam;
import com.bsj.power.common.def.dto.video.CmdToMqDto;
import com.bsj.power.common.util.DateUtils;
import com.bsj.power.common.util.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.validation.constraints.NotNull;
import java.util.Date;

@Slf4j
@Component
public class VideoCmdUntil {

    @Autowired
    private BaseMqPushUtil mqUntil;

    /**
     * 直播流端口
     */
    @Value("${video.rtpPort}")
    public Integer rtpPort;
    /**
     * 回放流端口
     */
    @Value("${video.rtpBackPort}")
    public Integer rtpBackPort;
    /**
     * 客户端结流HTTP长连接端口
     */
    @Value("${video.httpPort}")
    public Integer httpPort;

    public void realVideoReq(RealVideoParam param) {
        Integer channelNo = param.getChannelNo();
        if (channelNo != 0) {
            realVideoReq(param, channelNo, null);
        } else {
            // 通道为0，发送四路直播请求
            for (int i = 1; i < 5; i++) {
                // 组数据(获取服务器配置信息)
                param.setChannelNo(i);
                realVideoReq(param, param.getChannelNo(), null);
            }
        }
    }

    /**
     * 直播指令开启
     * @param param
     * @param channelNo
     */
    public void realVideoReq(RealVideoParam param, Integer channelNo, String resolutionRatio) {
        Integer cmd = 0;
        String terminalNo = param.getTerminalNo();
        String uuid = UUIDUtil.getUuid();
        CmdToMqDto mqDto = new CmdToMqDto(Long.parseLong(terminalNo), terminalNo, cmd, CmdConstant.PLAY, uuid, channelNo);
        if (resolutionRatio != null) {
            mqDto.setResolutionRatio(resolutionRatio);
        }
        mqUntil.sendTopicBytes(MqTopicAndTagConstants.MEDIA_SERVER_CHANGE_TOPIC,
                MqTopicAndTagConstants.MEDIA_LIVE_STREAM_OPEN_TAG, JSON.toJSONString(mqDto).getBytes());
    }

    public void closeConnection(CloseConnectionParam param) {
        log.info("关闭视频请求参数:{}", JSON.toJSONString(param));
        //获取redis中值terminal-channelNo为什么链接
        String terminalNo = param.getTerminalNo();
        Integer channelNo = param.getChannelNo();
        if (channelNo != 0) {
            int changeChannel = channelNo - 126;
            if (changeChannel < 0) {
                int controlTypeInt = param.getControlType() == null ? 0 : param.getControlType().intValue();
                //直播
                RealCtrolParam controlVO = new RealCtrolParam();
                controlVO.setChannelNo(channelNo);
                controlVO.setControlType(controlTypeInt);
                if (controlTypeInt == 4) {
                    controlVO.setCloseType(1);
                    controlVO.setStreamType(1);
                } else {
                    controlVO.setCloseType(0);
                    controlVO.setStreamType(0);
                }
                controlVO.setTerminalNo(terminalNo);
                //请求
                realControlReq(controlVO);
            } else if (changeChannel > 0) {
                //回放
                BackVodCtrolParam backVodCtrolParam = new BackVodCtrolParam();
                backVodCtrolParam.setChannelNo(changeChannel);
                backVodCtrolParam.setSpeed(0);
                backVodCtrolParam.setControlType(2);
                backVodCtrolParam.setControl(2);
                backVodCtrolParam.setDropTime(DateUtils.dateToStr(new Date()));
                backVodCtrolParam.setTerminalNo(terminalNo);
                //请求
                replayControlReq(backVodCtrolParam);
            } else {
                throw new ApiException(JsonResultEnum.FAIL);
            }

        } else {
            for (int i = 1; i < 5; i++) {
                RealCtrolParam controlVO = new RealCtrolParam();
                controlVO.setChannelNo(i);
                controlVO.setControlType(0);
                controlVO.setCloseType(0);
                controlVO.setStreamType(0);
                controlVO.setTerminalNo(terminalNo);
                //请求
                realControlReq(controlVO);
            }
        }
    }

    /**
     * 直播流关闭
     * @param param
     */
    public void realControlReq(RealCtrolParam param) {
        Integer channelNo = param.getChannelNo();
        String terminalNo = param.getTerminalNo();
        //清空缓存的RTSP流数据的链路
        Integer cmd = param.getCloseType();
        String uuid = UUIDUtil.getUuid();
        CmdToMqDto mqDto = new CmdToMqDto(Long.parseLong(terminalNo), terminalNo, cmd, CmdConstant.CLOSE, uuid, channelNo);
        log.info("发送直播关闭指令: {}", param.toString());
        mqUntil.sendTopicBytes(MqTopicAndTagConstants.MEDIA_SERVER_CHANGE_TOPIC,
                MqTopicAndTagConstants.MEDIA_LIVE_STREAM_OPEN_TAG, JSON.toJSONString(mqDto).getBytes());
    }


    /**
     * 回放关闭和控制
     * @param param
     */
    public void replayControlReq(BackVodCtrolParam param) {
        //清空缓存的RTSP流数据的链路
//        NettyUDPIntercomUntil.del(param.getTerminalNo());
        //指令类型
        Integer cmd = 1;
        BackVodControl request = new BackVodControl(param);
        CmdToMqDto mqDto = new CmdToMqDto(Long.parseLong(param.getTerminalNo()), param.getTerminalNo(), cmd, param.getControl());
        mqDto.setBackVodControl(request);
        mqUntil.sendTopicBytes(MqTopicAndTagConstants.MEDIA_SERVER_CHANGE_TOPIC,
                MqTopicAndTagConstants.MEDIA_LIVE_BACK_OPEN_TAG, JSON.toJSONString(mqDto).getBytes());
    }

    /**
     * 视频回放开启指令
     * @param param
     */
    public void replayReq(BackVodParam param) {
        //回放
        Integer cmd = 1;
        String terminalNo = param.getTerminalNo();
        Integer channelNo = param.getChannelNo();
        String uuid = UUIDUtil.getUuid();
        CmdToMqDto mqDto = new CmdToMqDto(Long.parseLong(terminalNo), terminalNo, cmd, CmdConstant.PLAY, uuid, channelNo);
        String startTime = param.getStartTime();
        String endTime = param.getEndTime();
//        Long s = Long.parseLong(startTime) + 120L;
//        Long e = Long.parseLong(endTime) + 120L;
        Long s = Long.parseLong(startTime);
        Long e = Long.parseLong(endTime);
        param.setStartTime(s + "");
        param.setEndTime(e + "");
        mqDto.setBackVodParam(param);
        mqUntil.sendTopicBytes(MqTopicAndTagConstants.MEDIA_SERVER_CHANGE_TOPIC,
                MqTopicAndTagConstants.MEDIA_LIVE_BACK_OPEN_TAG, JSON.toJSONString(mqDto).getBytes());
    }
}
